package cn.iocoder.yudao.module.cockpit.service.areatotal;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cockpit.controller.admin.areatotal.vo.AreaTotalPageReqVO;
import cn.iocoder.yudao.module.cockpit.controller.admin.areatotal.vo.AreaTotalSaveReqVO;
import cn.iocoder.yudao.module.cockpit.dal.dataobject.areatotal.AreaTotalDO;
import cn.iocoder.yudao.module.cockpit.dal.mysql.areatotal.AreaTotalMapper;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cockpit.enums.ErrorCodeConstants.AREA_TOTAL_NOT_EXISTS;

/**
 * 区域统计 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AreaTotalServiceImpl implements AreaTotalService {

    @Resource
    private AreaTotalMapper areaTotalMapper;

    @Override
    public Long createAreaTotal(AreaTotalSaveReqVO createReqVO) {
        // 插入
        AreaTotalDO areaTotal = BeanUtils.toBean(createReqVO, AreaTotalDO.class);
        areaTotalMapper.insert(areaTotal);
        // 返回
        return areaTotal.getId();
    }

    @Override
    public void updateAreaTotal(AreaTotalSaveReqVO updateReqVO) {
        // 校验存在
        validateAreaTotalExists(updateReqVO.getId());
        // 更新
        AreaTotalDO updateObj = BeanUtils.toBean(updateReqVO, AreaTotalDO.class);
        areaTotalMapper.updateById(updateObj);
    }

    @Override
    public void deleteAreaTotal(Long id) {
        // 校验存在
        validateAreaTotalExists(id);
        // 删除
        areaTotalMapper.deleteById(id);
    }

    private void validateAreaTotalExists(Long id) {
        if (areaTotalMapper.selectById(id) == null) {
            throw exception(AREA_TOTAL_NOT_EXISTS);
        }
    }

    @Override
    public AreaTotalDO getAreaTotal(Long id) {
        return areaTotalMapper.selectById(id);
    }

    @Override
    public PageResult<AreaTotalDO> getAreaTotalPage(AreaTotalPageReqVO pageReqVO) {
        return areaTotalMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AreaTotalDO> getList(AreaTotalPageReqVO pageReqVO) {
        return areaTotalMapper.selectList();
    }
}